﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NapCardOnline.Models;
using PagedList;

namespace NapCardOnline.Controllers
{ 
    public class RechargeController : Controller
    {
        private NapCardOnlineEntities db = new NapCardOnlineEntities();

        //
        // GET: /Recharge/

        public ViewResult Index(int? page)
        {
            User objUser = Session["User"] as User;
            var recharges =
                db.Recharges.Include("BankAccountCompany").Include("Bank").Include("RechargeType").Include("User").Where(t => t.FK_ToUserID == objUser.UserID || t.FK_FromUserID==objUser.UserID).OrderByDescending(t => t.RechargeDate);

            var pageNumber = page ?? 1;
            var onePageOfRecharge = recharges.ToPagedList(pageNumber, 10);
            ViewBag.OnePageOfRecharge = onePageOfRecharge;
            ViewBag.CountRecharge = recharges.Count();
            ViewBag.TotalAmount = recharges.Sum(t => t.RechargeAmount);
            return View(onePageOfRecharge);
        }

        //
        // GET: /Recharge/Details/5

        public ViewResult Details(int id)
        {
            Recharge recharge = db.Recharges.Single(r => r.RechargeID == id);
            return View(recharge);
        }

        //
        // GET: /Recharge/Create

        public ActionResult Create()
        {
            ViewBag.FK_BankAccountCompanyID = new SelectList(db.BankAccountCompanys, "BankAccountCompanyID", "BankAccountCompanyNumber");
            ViewBag.FK_BankID = new SelectList(db.Banks, "BankID", "BankName");
            ViewBag.FK_RechargeTypeID = new SelectList(db.RechargeTypes, "RechargeTypeID", "RechargeTypeName");
            ViewBag.FK_UserID = new SelectList(db.Users, "UserID", "UserName");
            return View();
        } 

        //
        // POST: /Recharge/Create

        [HttpPost]
        public ActionResult Create(Recharge recharge)
        {
            if (ModelState.IsValid)
            {
                var objUserFrom = Session["User"] as Models.User;
                if (objUserFrom != null)
                {
                    recharge.FK_FromUserID = objUserFrom.UserID;
                    recharge.FK_ToUserID = 1;
                    recharge.RechargeDate = DateTime.Now;
                    recharge.FK_StatusID = (int)Enum.Status.Success;
                    db.Recharges.AddObject(recharge);
                    db.SaveChanges();
                    return View(recharge=null);
                }
            }

            ViewBag.FK_BankAccountCompanyID = new SelectList(db.BankAccountCompanys, "BankAccountCompanyID", "BankAccountCompanyNumber", recharge.FK_BankAccountCompanyID);
            ViewBag.FK_BankID = new SelectList(db.Banks, "BankID", "BankName", recharge.FK_BankID);
            ViewBag.FK_RechargeTypeID = new SelectList(db.RechargeTypes, "RechargeTypeID", "RechargeTypeName", recharge.FK_RechargeTypeID);
            ViewBag.FK_UserID = new SelectList(db.Users, "UserID", "UserName", recharge.FK_ToUserID);
            return View(recharge);
        }
        
        //
        // GET: /Recharge/Edit/5
 
        public ActionResult Edit(int id)
        {
            Recharge recharge = db.Recharges.Single(r => r.RechargeID == id);
            ViewBag.FK_BankAccountCompanyID = new SelectList(db.BankAccountCompanys, "BankAccountCompanyID", "BankAccountCompanyNumber", recharge.FK_BankAccountCompanyID);
            ViewBag.FK_BankID = new SelectList(db.Banks, "BankID", "BankNo", recharge.FK_BankID);
            ViewBag.FK_RechargeTypeID = new SelectList(db.RechargeTypes, "RechargeTypeID", "RechargeTypeName", recharge.FK_RechargeTypeID);
            ViewBag.FK_UserID = new SelectList(db.Users, "UserID", "UserNo", recharge.FK_ToUserID);
            return View(recharge);
        }

        //
        // POST: /Recharge/Edit/5

        [HttpPost]
        public ActionResult Edit(Recharge recharge)
        {
            if (ModelState.IsValid)
            {
                db.Recharges.Attach(recharge);
                db.ObjectStateManager.ChangeObjectState(recharge, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.FK_BankAccountCompanyID = new SelectList(db.BankAccountCompanys, "BankAccountCompanyID", "BankAccountCompanyNumber", recharge.FK_BankAccountCompanyID);
            ViewBag.FK_BankID = new SelectList(db.Banks, "BankID", "BankNo", recharge.FK_BankID);
            ViewBag.FK_RechargeTypeID = new SelectList(db.RechargeTypes, "RechargeTypeID", "RechargeTypeName", recharge.FK_RechargeTypeID);
            ViewBag.FK_UserID = new SelectList(db.Users, "UserID", "UserNo", recharge.FK_ToUserID);
            return View(recharge);
        }

        //
        // GET: /Recharge/Delete/5
 
        public ActionResult Delete(int id)
        {
            Recharge recharge = db.Recharges.Single(r => r.RechargeID == id);
            return View(recharge);
        }

        //
        // POST: /Recharge/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Recharge recharge = db.Recharges.Single(r => r.RechargeID == id);
            db.Recharges.DeleteObject(recharge);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}